package king.dominic.jlibrary.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;

import java.util.Set;

/**
 * Created by king on 2017/1/24.
 * Preference
 */

public class Preference {
    public static final String DEFAULT_STRING = "";
    public static final long DEFAULT_LONG = -1;
    private static SharedPreferences mPreference;

    public static void init(Context context) {
        mPreference = context.getSharedPreferences(context.getPackageName() + ".config", Context.MODE_PRIVATE);
    }
    public static SharedPreferences get(){
        return mPreference;
    }
    public static synchronized boolean setConfig(String key, String value) {
        SharedPreferences.Editor edit = mPreference.edit();
        edit.putString(key, value);
        return edit.commit();
    }

    public static synchronized void put(String key, String value) {
        SharedPreferences.Editor edit = mPreference.edit();
        edit.putString(key, value);
        edit.apply();
    }

    public static synchronized void put(String key, int value) {
        SharedPreferences.Editor edit = mPreference.edit();
        edit.putInt(key, value);
        edit.apply();
    }

    public static synchronized void put(String key, boolean value) {
        SharedPreferences.Editor edit = mPreference.edit();
        edit.putBoolean(key, value);
        edit.apply();
    }

    public static synchronized void put(String key, float value) {
        SharedPreferences.Editor edit = mPreference.edit();
        edit.putFloat(key, value);
        edit.apply();
    }

    public static synchronized void put(String key, long value) {
        SharedPreferences.Editor edit = mPreference.edit();
        edit.putLong(key, value);
        edit.apply();
    }

    public static synchronized String get(String key, String defValue) {
        return mPreference.getString(key, defValue);
    }

    public static synchronized int get(String key, int defValue) {
        return mPreference.getInt(key, defValue);
    }

    public static synchronized boolean get(String key, boolean defValue) {
        return mPreference.getBoolean(key, defValue);
    }

    public static synchronized float get(String key, float defValue) {
        return mPreference.getFloat(key, defValue);
    }

    public static synchronized long get(String key, long defValue) {
        return mPreference.getLong(key, defValue);
    }

    public static class EditorCommitter {
        private final SharedPreferences.Editor editor;

        @SuppressLint("CommitPrefEdits")
        public EditorCommitter(SharedPreferences preferences) {
            editor = preferences.edit();
        }

        public EditorCommitter put(String key, String value) {
            editor.putString(key, value);
            return this;
        }

        public EditorCommitter put(String key, int value) {
            editor.putInt(key, value);
            return this;
        }

        public EditorCommitter put(String key, float value) {
            editor.putFloat(key, value);
            return this;
        }

        public EditorCommitter put(String key, boolean value) {
            editor.putBoolean(key, value);
            return this;
        }

        public EditorCommitter put(String key, long value) {
            editor.putLong(key, value);
            return this;
        }

        public EditorCommitter put(String key, Set<String> value) {
            editor.putStringSet(key, value);
            return this;
        }

        public boolean commit() {
            return editor.commit();
        }

        public void apply() {
            editor.apply();
        }
    }
}
